﻿namespace ctolua.Models.UI
{
    /// <summary>
    /// 场景UI
    /// </summary>
    public class MScene
    {
        /// <summary>
        /// 场景点
        /// </summary>
        public SceneNode scene { get; set; }
        /// <summary>
        /// 模板UID
        /// </summary>
        public string uiId { get; set; }

        private int? _visible_dis { get; set; }
        /// <summary>
        /// 可见距离
        /// </summary>
        public int? visible_dis
        {
            get => this._visible_dis;
            set
            {
                if (value != null)
                {
                    _visible_dis = value;
                }
                else
                {
                    _visible_dis = 9999;
                }

            }
        }
        private int? _height_offset { get; set; }
        /// <summary>
        /// 离地高度
        /// </summary>
        public int? height_offset
        {
            get => this._height_offset;
            set
            {
                if (value != null)
                {
                    _height_offset = value;
                }
                else
                {
                    _height_offset = 200;
                }

            }
        }


        /// <summary>
        /// 创建场景点并绑定UI到点
        /// </summary>
        /// <param name="p">点</param>
        /// <param name="s"></param>
        public MScene(Point p, string uid, int? _visible_dis=null, int? _height_offset = null)
        {
            this.visible_dis = _visible_dis;
            this.height_offset = _height_offset;
            this.uiId = uid;

            this.scene = game_api.create_scene_node_on_point(this.uiId, p, this.visible_dis, this.height_offset);
        }

        /// <summary>
        /// 创建场景点并绑定UI到单位
        /// </summary>
        /// <param name="u">单位</param>
        /// <param name="r">玩家</param>
        /// <param name="socket_name">挂接点</param>
        /// <param name="s"></param>
        public MScene(MUnit u, MRole r, string socket_name, string uid, int? _visible_dis = null, int? _height_offset = null)
        {
            this.visible_dis = _visible_dis;
            this.height_offset = _height_offset;
            this.uiId = uid;

            this.scene = game_api.create_scene_node_on_unit(this.uiId, r.role, u.unit, socket_name, this.visible_dis);
        }

        

        /// <summary>
        /// 获得场景ui中的控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T get_ui<T>() where T : Mui, new()
        {
            T ui = new T();
            ui.id = game_api.get_ui_comp_in_scene_ui(this.scene, this.uiId);
            return ui;
        }

        /// <summary>
        /// 删除场景点
        /// </summary>
        public void del() => game_api.get_ui_comp_in_scene_ui(this.scene, this.uiId);

    }
}
